<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>libdill</title>
  <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>

<h1>libdill: Structured Concurrency for C</h1>

<ul id='toplist'>
<li><a href="index.html">Home</a></li>
<li><a href="download.html">Download</a></li>
<li><a href="documentation.html">Documentation</a></li>
<li><a href="tutorial.html">Tutorials</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
<h1 id="name">NAME</h1>
<p>http_attach - creates HTTP protocol on top of underlying socket</p>
<h1 id="synopsis">SYNOPSIS</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="pp">#include </span><span class="im">&lt;libdill.h&gt;</span>

<span class="dt">int</span> http_attach(
    <span class="dt">int</span> s);</code></pre></div>
<h1 id="description">DESCRIPTION</h1>
<p><strong>WARNING: This is experimental functionality and the API may change in the future.</strong></p>
<p>HTTP is an application-level protocol described in RFC 7230. This implementation handles only the request/response exchange. Whatever comes after that must be handled by a different protocol.</p>
<p>This function instantiates HTTP protocol on top of the underlying protocol.</p>
<p><strong>s</strong>: Handle of the underlying socket. It must be a bytestream protocol.</p>
<p>The socket can be cleanly shut down using <strong>http_detach</strong> function.</p>
<p>This function is not available if libdill is compiled with <strong>--disable-sockets</strong> option.</p>
<h1 id="return-value">RETURN VALUE</h1>
<p>In case of success the function returns newly created socket handle. In case of error it returns -1 and sets <strong>errno</strong> to one of the values below.</p>
<h1 id="errors">ERRORS</h1>
<ul>
<li><strong>EBADF</strong>: Invalid handle.</li>
<li><strong>EMFILE</strong>: The maximum number of file descriptors in the process are already open.</li>
<li><strong>ENFILE</strong>: The maximum number of file descriptors in the system are already open.</li>
<li><strong>ENOMEM</strong>: Not enough memory.</li>
<li><strong>ENOTSUP</strong>: The handle does not support this operation.</li>
<li><strong>EPROTO</strong>: Underlying socket is not a bytestream socket.</li>
</ul>
<h1 id="example">EXAMPLE</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> s = tcp_connect(&amp;addr, <span class="dv">-1</span>);
s = http_attach(s);
http_sendrequest(s, <span class="st">&quot;GET&quot;</span>, <span class="st">&quot;/&quot;</span>, <span class="dv">-1</span>);
http_sendfield(s, <span class="st">&quot;Host&quot;</span>, <span class="st">&quot;www.example.org&quot;</span>, <span class="dv">-1</span>);
http_done(s, <span class="dv">-1</span>);
<span class="dt">char</span> reason[<span class="dv">256</span>];
http_recvstatus(s, reason, <span class="kw">sizeof</span>(reason), <span class="dv">-1</span>);
<span class="cf">while</span>(<span class="dv">1</span>) {
    <span class="dt">char</span> name[<span class="dv">256</span>];
    <span class="dt">char</span> value[<span class="dv">256</span>];
    <span class="dt">int</span> rc = http_recvfield(s, name, <span class="kw">sizeof</span>(name), value, <span class="kw">sizeof</span>(value), <span class="dv">-1</span>);
    <span class="cf">if</span>(rc == <span class="dv">-1</span> &amp;&amp; errno == EPIPE) <span class="cf">break</span>;
}
s = http_detach(s, <span class="dv">-1</span>);
tcp_close(s);</code></pre></div>
<h1 id="see-also">SEE ALSO</h1>
<p><strong>hclose</strong>(3) <strong>http_attach_mem</strong>(3) <strong>http_detach</strong>(3) <strong>http_done</strong>(3) <strong>http_recvfield</strong>(3) <strong>http_recvrequest</strong>(3) <strong>http_recvstatus</strong>(3) <strong>http_sendfield</strong>(3) <strong>http_sendrequest</strong>(3) <strong>http_sendstatus</strong>(3)</p>
</body>
